package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.bean.CartBean;
import com.qf.pojo.Product;
import com.qf.pojo.User;
import com.qf.service.CartService;
import com.qf.service.Impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @author yangl
 * @version 1.0
 * @date 2023/4/26 11:38
 */

@WebServlet("/cartFindAllController.action")
public class CartFindAllController extends HttpServlet {

    private CartService cartService = new CartServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从session中获取用户对象
        HttpSession session = req.getSession(false);
        User user = (User)session.getAttribute("user");
        //查询商品
        List<CartBean> cartBeanList =  cartService.findAll(user.getUid());
        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        //判断
        if(cartBeanList !=null && cartBeanList.size() > 0){
            //返回给前端
            map.put("data",cartBeanList);
        }
        //返回数据给前端
        resp.getWriter().write(JSON.toJSONString(map));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
